/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */
using System;

namespace Pbdms.BusinessLogic.RSS
{
	/// <summary>
	/// Represents a FeedEntry
	/// </summary>
	[Serializable]
	public class FeedEntry  {
		private string mTitle;
		private string mLink;
		private string mDescription;
		private string mSubject;
		private DateTime mPublicationDate;
		private RssGuid mRssGuid;
		private string mCreator;
		private bool mIsRead;
		[NonSerialized] Subscription mSubscription;

		public FeedEntry() {
			mRssGuid = new RssGuid();
			mTitle = "";
			mLink = "";
			mDescription = "";
			mSubject = "";
			mPublicationDate = DateTime.Now;
			mCreator = "";
			mIsRead = false;
			mSubscription = null;
		}	

		public FeedEntry(Subscription subscription) : this() {
			mSubscription = subscription;
		}	

		public Subscription subscription {
			get { return mSubscription; }
			set { mSubscription = value; }
		}

		/// <summary>
		/// Application specific meta data. Not part of Equals or GetHashCode (maybe it should be?)
		/// </summary>
		public bool isRead {
			get { return mIsRead; }
			set { mIsRead = value; }
		}

		public string title {
			get { return mTitle; }
			set { mTitle = value; }
		}
		
		public string link {
			get { return mLink; }
			set { mLink = value; }
		}

		public string description {
			get { return mDescription; }
			set { mDescription = value; }
		}

		public string subject {
			get { return mSubject; }
			set { mSubject = value; }
		}

		public string creator {
			get { return mCreator; }
			set { mCreator = value; }
		}

		public DateTime publicationDate {
			get { return mPublicationDate; }
			set { mPublicationDate = value; }
		}

		public RssGuid rssGuid {
			get { return mRssGuid; }
			set { mRssGuid = value; }
		}

		public string cacheKey {
            get { return 
               mTitle
		+" "+ mLink
		+" "+mDescription
		+" "+mSubject
		+" "+mPublicationDate.ToString("r");
                
                ; }
		}

		public override string ToString() {
			string returnValue;
			returnValue = "Subject: " + subject + "\r\n";
			returnValue += "Title: " + title + "\r\n";
			returnValue += "Link: " + link + "\r\n";
			returnValue += "Description: " + description + "\r\n";
			returnValue += "PublicationDate: " + mPublicationDate.ToString() + "\r\n";
			returnValue += "RssGuid: " + rssGuid.ToString() + "\r\n";
			returnValue += "Creator: " + creator + "\r\n";
			returnValue += "IsRead: " + isRead;
			return returnValue;
		}


		public override bool Equals(object obj) {
			if (obj is FeedEntry) {
				FeedEntry other = (FeedEntry)obj;
				if (other.creator == this.creator &&
					other.description == this.description &&
					other.link == this.link &&
					other.publicationDate == this.publicationDate &&
					other.rssGuid.Equals(this.rssGuid) && 
					other.subject == this.subject &&
					other.title == this.title) {
					return true;
				}
			}
			return false;
		}

		public override int GetHashCode() {
			int result = 17;
			result = 37*result + this.creator.GetHashCode();
			result = 37*result + this.description.GetHashCode();
			result = 37*result + this.link.GetHashCode();
			result = 37*result + this.publicationDate.GetHashCode();
			result = 37*result + this.rssGuid.GetHashCode();
			result = 37*result + this.subject.GetHashCode();
			result = 37*result + this.title.GetHashCode();
			return result;
		}


		/// <summary>
		/// Represents an RSS Guid
		/// </summary>
		[Serializable]
			public class RssGuid {
			private bool mIsPermaLink = true;
			private string mGuid = "";

			public RssGuid() {
			}

			public RssGuid(string guid, bool isPermaLink) {
				mGuid = guid;
				mIsPermaLink = isPermaLink;
			}

			public bool isPermaLink {
				get { return mIsPermaLink; }
				set { mIsPermaLink = value; }
			}

			public string guid {
				get { return mGuid; }
				set { mGuid = value; }
			}

			public override string ToString() {
				return "Guid (isPermaLink="+isPermaLink+"): " + guid;
			}

			public override bool Equals(object obj) {
				if (obj is RssGuid) {
					RssGuid other = (RssGuid)obj;
					if (other.guid == this.guid &&
						other.isPermaLink == this.isPermaLink) {
						return true;
					}
				}
				return false;
			}

			public override int GetHashCode() {
				int result = 17;
				result = 37*result + guid.GetHashCode();
				if (isPermaLink) {
					result = 37*result + 1;
				} 
				return result;
			}
		}
	}

}
